import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:s_k_todo/model/task_model.dart';
import 'package:s_k_todo/provider/task_info_provider.dart';

class TaskInfoPage extends StatefulWidget {
  final int taskID;

  const TaskInfoPage(this.taskID, {super.key});

  @override
  State<TaskInfoPage> createState() => _TaskInfoPageState();
}

class _TaskInfoPageState extends State<TaskInfoPage> {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (ctx) => TaskInfoProvider(widget.taskID),
      builder: (ctx, _) => const _TaskInfoPage(),
    );
  }
}

class _TaskInfoPage extends StatelessWidget {
  const _TaskInfoPage({super.key});

  @override
  Widget build(BuildContext context) {
    TaskInfoProvider provider = context.watch();
    String title = provider.task?.taskTitle ?? "";
    String tips = provider.task?.tips ?? "";
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        shadowColor: Colors.transparent,
        elevation: 0,
        scrolledUnderElevation: 0,
        title: const Text("TaskInfo"),
        actions: [Switch(value: false, onChanged: (select) {})],
      ),
      body: CustomScrollView(
        slivers: [
          SliverPadding(
            padding: const EdgeInsets.symmetric(horizontal: 15),
            sliver: SliverToBoxAdapter(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title ?? "",
                    style: Theme.of(context).textTheme.titleLarge,
                  ),
                  const SizedBox(
                    height: 15,
                  ),
                  Text("备注：${tips ?? ""}"),
                  SizedBox(
                    height: 15,
                  ),
                  Text(
                    "实现步骤：",
                    style: Theme.of(context).textTheme.titleMedium,
                  ),
                  const SizedBox(
                    height: 15,
                  ),
                ],
              ),
            ),
          ),
          SliverPadding(
            padding: EdgeInsets.symmetric(horizontal: 15).copyWith(bottom: 20),
            sliver: SliverList.separated(
              itemBuilder: (ctx, index) =>
                  _StepItem(provider.task?.stepList?[index]),
              separatorBuilder: (ctx, index) => const SizedBox(
                height: 15,
              ),
              itemCount: provider.task?.stepList?.length ?? 0,
            ),
          )
        ],
      ),
    );
  }
}

class _StepItem extends StatelessWidget {
  final TaskStep? step;

  const _StepItem(this.step, {super.key});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Card(
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
            child: Row(
              children: [
                Expanded(
                    child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      step?.stepTitle ?? "",
                      style: Theme.of(context).textTheme.bodyLarge,
                    )
                  ],
                )),
                Column(
                  children: [
                    MaterialButton(
                      onPressed: () {},
                      child: Text("删除"),
                      textColor: Color(0xFF666666),
                      hoverColor: Colors.transparent,
                      hoverElevation: 0,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(5)),
                    ),
                    SizedBox(
                      height: 12,
                    ),
                    MaterialButton(
                      onPressed: () {},
                      child: Text("完成"),
                      hoverColor: Colors.transparent,
                      hoverElevation: 0,
                    )
                  ],
                ),
              ],
            ),
          ),
        ),
        const Positioned(
          right: 0,
          top: 0,
          child: Banner(message: "未完成", location: BannerLocation.topEnd),
        )
      ],
    );
  }
}
